package kr.or.javacafe.demo.controller;

import java.util.List;
import java.util.Locale;

import kr.or.javacafe.demo.domain.*;
import kr.or.javacafe.demo.domain.mongo.*;
import kr.or.javacafe.demo.domain.rank.*;
import kr.or.javacafe.demo.domain.rss.*;
import kr.or.javacafe.demo.service.*;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/app/demo")
public class DemoController {
	
	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
		
	@Autowired
	DemoService demoService;
	

	
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String main(Locale locale, Model model) {		
		return "demo/main";
	}
	

	@RequestMapping(value = "/location", method = RequestMethod.GET)
	public String location(Locale locale, Model model) {
		return "demo/location";
	}


	@RequestMapping(value = "/naverDiary", method = RequestMethod.GET)
	public String naverDiary(Locale locale, Model model) {
		
		RssData param = new RssData();
		param.setServiceName("NAVER_DIARY");
		param.setServiceKey("http://naver_diary.blog.me/rss");
		
		RssData naverDiary = demoService.getRssResult(param);
		model.addAttribute("_naverDiary", naverDiary);
		
		return "demo/naverDiary";
	}


	@RequestMapping(value = "/rank", method = RequestMethod.GET)
	public String rank(Locale locale, Model model) {

		RankData param = new RankData();
		param.setServiceName("NAVER_RANK");
		param.setServiceKey("http://openapi.naver.com/search?query=nexearch&target=rank&key=c1b406b32dbbbbeee5f2a36ddc14067f");
		
		RankData rank = demoService.getRankResult(param);
		model.addAttribute("_rank", rank);
		
		return "demo/rank";
	}

	
	@RequestMapping(value = "/accessLog", method = RequestMethod.GET)
	public String accessLog(Locale locale, Model model) {
		
		List<AccessLog> objList = demoService.getAccessLog();
		model.addAttribute("_logList", objList);
		
		return "demo/accessLog";
	}
	
	@RequestMapping(value="/maleMemver", method = RequestMethod.GET)
	public String viewMaleMember(Locale locale, Model model) {
		
		List<StudyMember> objList = demoService.getStudyMember();
		model.addAttribute("memList", objList);
		
		return "demo/maleMember";
	}
	
	@RequestMapping(value = "/saveStudyMember", method = RequestMethod.GET)
	public String saveStudyMember(Locale locale, Model model) {
		
		StudyMember sm1 = new StudyMember();
		sm1.setName("김미미");
		sm1.setAge("20");
		sm1.setSex("female");
		
		StudyMember sm2 = new StudyMember();
		sm2.setName("김철");
		sm2.setAge("25");
		sm2.setSex("male");
		
		demoService.saveStudyMember(sm1);
		demoService.saveStudyMember(sm2);
		
		return "demo/saveStudyMember";
	}


	@RequestMapping(value = "/searchTwitter", method = RequestMethod.GET)
	public String searchTwitter(@RequestParam(value="word",required=false) String word, Model model) {
		
		Long totalCount = demoService.getTwitterWordTotalCount();
		model.addAttribute("_totalCount", totalCount);

		List<TwitterWord> objBestWordList = demoService.findBestTwitterWord();
		model.addAttribute("_bestWordList", objBestWordList);			
		
		if (StringUtils.isNotEmpty(word)) {
			List<TwitterWord> objList = demoService.findTwitterWord(word);
			model.addAttribute("_wordList", objList);			
		}
		
		return "demo/searchTwitter";
	}

	
	@RequestMapping(value = "/searchTwitterRawData", method = RequestMethod.GET)
	public String searchTwitterRawData(@RequestParam(value="word",required=false) String word, Model model) {
		
		Long totalCount = demoService.getTwitterRawDataTotalCount();
		model.addAttribute("_totalCount", totalCount);
		
		if (StringUtils.isNotEmpty(word)) {
			List<TwitterRawData> objList = demoService.findTwitterRawData(word);
			model.addAttribute("_tweetList", objList);
			model.addAttribute("_searchWord", word);
		}
		
		return "demo/searchTwitterRawData";
	}
	
	
}














